//
//  MyRatingCtryViewController.m
//  ZHZtest
//
//  Created by 朱圳 on 2017/3/14.
//  Copyright © 2017年 朱圳. All rights reserved.
//

#define RGB(x,y,z) [UIColor colorWithRed:x/255.0 green:y/255.0 blue:z/255.0 alpha:1.0]

#define randomColor [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1]
#import "MyRatingCtry.h"
#import "StuPersonEduViewController.h"
#import "TestViewController.h"
#import "QuestionsModel.h"
#import "ZFChart.h"
#import "ComentPieCell.h"
#import "YHClockhead.h"

#import "MyRatingListVC.h"//跳转评价等级
#import "WSDatePickerView.h"//日期
@interface MyRatingCtry ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)UIView *noDataView;
@property (nonatomic,strong)NSMutableArray *questionArray;
@property (nonatomic, strong) ZFPieChart * pieChart;
@property (nonatomic, strong) NSString *year;
@property (nonatomic, strong) NSString *month;

@end

@implementation MyRatingCtry

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setrightButton];
    [self setLimit];
}
-(void)viewWillAppear:(BOOL)animated{
        [super viewWillAppear:animated];
    [self requestComentDetail];
}

-(void)setLimit{
    NSString *dateString = [[NSDate date] stringWithFormat:@"yyyy-M-d"];
    NSArray *date = [dateString componentsSeparatedByString:@"-"];
    self.year = date[0];
    NSString *day = date[2];
    if ([day doubleValue]<27) {
        self.month = [NSString stringWithFormat:@"%.f",[date[1] doubleValue] - 1] ;
        if ([self.month doubleValue]<1) {
            self.month = @"12";
            self.year = [NSString stringWithFormat:@"%.f",[date[0] doubleValue] - 1];
        }
    }else{
        self.month = date[1];
        [self requestComentDetail];
    }
    self.title = [NSString stringWithFormat:@"%@月评价",self.month];
}
-(void)setrightButton{
    
    UIButton*rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0,0,30,30)];
    [rightButton setImage:[UIImage imageNamed:@"时间2"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(search) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem*rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem= rightItem;
}
- (void)search{
    
    //年-月
    WSDatePickerView *datepicker = [[WSDatePickerView alloc] initWithDateStyle:DateStyleShowYearMonth CompleteBlock:^(NSDate *selectDate) {
        
        NSString *dateString = [selectDate stringWithFormat:@"yyyy-M"];
        NSArray *date = [dateString componentsSeparatedByString:@"-"];
        self.year = date[0];
        self.month = date[1];
        //NSLog(@"选择的日期：%@",dateString);
        self.title = [NSString stringWithFormat:@"%@月评价",self.month];
        [self requestComentDetail];
        
    }];
    NSDateFormatter *minDateFormater = [[NSDateFormatter alloc] init];
    [minDateFormater setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate *minLimitDate = [minDateFormater dateFromString:@"2018-2-1 00:00"];
    datepicker.maxLimitDate = [NSDate date];
    datepicker.minLimitDate = minLimitDate;
    datepicker.dateLabelColor = randomColor;//年-月-日-时-分 颜色
    datepicker.datePickerColor = randomColor;//滚轮日期颜色
    datepicker.doneButtonColor = randomColor;//确定按钮的颜色
    [datepicker show];
}

-(void)requestComentDetail{

    __weak typeof(self) weakSelf = self;
    NSDictionary *dict = @{
                           @"jwid" : JWid,
                           @"jwpassword" : JWpassword,
                           @"year" : self.year,
                           @"mounth" : self.month,
                           @"type" : @"1"
                           };
    [[AFNetworkTool shareManager]postJSONWithUrl:JW_Mounthevaluate parmas:dict successData:^(id json) {
        [self.questionArray removeAllObjects];
        NSArray * arr = json[@"list"][0];
        for (int i = 0; i<arr.count; i++) {
            QuestionsModel * model = [[QuestionsModel alloc]init];
            model.content = arr[i][0];
            model.questionID = arr[i][6];
            
            NSArray *starArr1 = arr[i][1];
            NSString * star1 =[NSString stringWithFormat:@"%lu",(long)starArr1.count];
            NSArray *starArr2 = arr[i][2];
            NSString * star2 =[NSString stringWithFormat:@"%lu",(long)starArr2.count];
            NSArray *starArr3 = arr[i][3];
            NSString * star3 =[NSString stringWithFormat:@"%lu",(long)starArr3.count];
            NSArray *starArr4 = arr[i][4];
            NSString * star4 =[NSString stringWithFormat:@"%lu",(long)starArr4.count];
            NSArray *starArr5 = arr[i][5];
            NSString * star5 =[NSString stringWithFormat:@"%lu",(long)starArr5.count];
            
            [model.starCountArr addObject:star1];
            [model.starCountArr addObject:star2];
            [model.starCountArr addObject:star3];
            [model.starCountArr addObject:star4];
            [model.starCountArr addObject:star5];
            //   NSLog(@">>aaa>%@", model.starCountArr);
            [weakSelf.questionArray addObject:model];
        }
        //        NSLog(@">a>%@", self.questionArray);
        if (arr.count > 0) {
            [self.view addSubview:self.tableView];
            [self.noDataView removeFromSuperview];
        }else{
            [self.tableView removeFromSuperview];
            [self.view addSubview:self.noDataView];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf.tableView reloadData];
        });
    } failure:^(NSError *error) {
        [self.view addSubview:self.noDataView];
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.questionArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    // 通过indexPath创建cell实例 每一个cell都是单独的
    ComentPieCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell) {
        cell = [[ComentPieCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    QuestionsModel *model = self.questionArray[indexPath.section];
    cell.model = model;
    return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    YHClockhead *head=[[YHClockhead alloc]init];
    QuestionsModel * model = self.questionArray[section];
    head.headcoursename.text= [NSString stringWithFormat:@"%ld.%@",(long)section+1,model.content];
    head.headcoursename.textAlignment = NSTextAlignmentLeft;
    head.headcoursename.font = [UIFont systemFontOfSize:14];
    return head;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    QuestionsModel *model = self.questionArray[indexPath.section];
    MyRatingListVC * vc = [[MyRatingListVC alloc]init];
    vc.questionID = model.questionID;
    [self.navigationController pushViewController:vc animated:YES];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 170;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[ComentPieCell class] forCellReuseIdentifier:@"comentPieCell"];
    }
    return _tableView;
}

-(UIView *)noDataView{
    
    if (!_noDataView) {
        _noDataView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height-48)];
        _noDataView.backgroundColor = [UIColor whiteColor];
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.image = [UIImage imageNamed:@"NoData"];
        [_noDataView addSubview:imageView];
        
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(_noDataView);
            make.top.mas_equalTo(100);
            make.width.mas_equalTo(100);
            make.height.mas_equalTo(100);
        }];
        
        UILabel * la = [[UILabel alloc]init];
        la.text = @"亲,没有评价信息!";
        la.textColor = [UIColor darkGrayColor];
        la.textAlignment = NSTextAlignmentCenter;
        [_noDataView addSubview:la];
        [la mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(_noDataView);
            [make.top.mas_equalTo(imageView.mas_bottom) setOffset:10];
            make.width.mas_equalTo(200);
        }];
    }
    return _noDataView;
}
- (NSMutableArray *)questionArray {
    if(!_questionArray) {
        _questionArray = [NSMutableArray array];
    }
    return _questionArray;
}
@end
